package com.example.administrator.shzs.activity;

import android.os.Bundle;
import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.map.TextureMapView;
import com.baidu.mapapi.model.LatLng;
import com.example.administrator.shzs.R;
import com.example.administrator.shzs.utils.ToastUtils;
import com.example.administrator.shzs.app.MyApplication;

/**
 * 地图定位
 */
public class BaiduMapLocation extends BaseActivity {


    private BaiduMap mBaiduMap;

    /**
     * 定位
     */
    private LocationClient mLocationClient;

    private TextureMapView mMapView;

    //是否第一次进入定位
    private boolean isFirstIn=true;

    /**
     *纬度
     */
    private double latitude;

    /**
     * 经度
     */
    private double  longitude;


    private String address;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_baidu_map);
        MyApplication.app.activityList.add(this);
        //初始化控件
        setViews();

        setLocation();
    }


    public void setLocation() {

        mLocationClient=new LocationClient(getApplicationContext());

        mLocationClient.registerLocationListener(new MyBDLocation());

        //定位参数
        LocationClientOption option=new LocationClientOption();
        option.setCoorType("bd09ll");
        option.setIsNeedAddress(true);
        option.setOpenGps(true);
        option.setScanSpan(1000);//一秒请求一次

        mLocationClient.setLocOption(option);


    }


    /**
     * 初始化控件
     */
    private void setViews() {

        mMapView= (TextureMapView) findViewById(R.id.bmapView);
        mBaiduMap=mMapView.getMap();

        //地图缩放比设置500米
        MapStatusUpdate msu= MapStatusUpdateFactory.zoomTo(15.0f);

        mBaiduMap.setMapStatus(msu);
    }



    class MyBDLocation implements BDLocationListener{

        @Override
        public void onReceiveLocation(BDLocation bdLocation) {
            //定位成功后的一个回调

            latitude=bdLocation.getLatitude();
            longitude=bdLocation.getLongitude();

            MyLocationData data=new MyLocationData.Builder()//
            .accuracy(bdLocation.getRadius())//
            .latitude(latitude)//
            .longitude(longitude)//
            .build();
            mBaiduMap.setMyLocationData(data);

            if(isFirstIn){//第一次进入定位
                LatLng latLng=new LatLng(latitude,longitude);
                MapStatusUpdate msu=MapStatusUpdateFactory.newLatLng(latLng);
                mBaiduMap.setMapStatus(msu);
                isFirstIn=false;

                address=bdLocation.getAddrStr();
                ToastUtils.showMessageLong(BaiduMapLocation.this,address);
            }
        }
    }

    @Override
    protected void onStart() {
        super.onStart();

        //开启定位允许
        mBaiduMap.setMyLocationEnabled(true);
        if(!mLocationClient.isStarted()){
        mLocationClient.start();
        }

    }


    @Override
    protected void onStop() {
        super.onStop();

        //关闭定位
        mBaiduMap.setMyLocationEnabled(false);
        mLocationClient.stop();

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mMapView.onDestroy();


    }

    @Override
    protected void onResume() {
        super.onResume();
        mMapView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mMapView.onPause();
    }
}
